﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Resources;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Report;

public partial class admin_view_agent_target_amount : System.Web.UI.Page
{
    string PageCode = "GGRPTRADBRVTA";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                CheckPageAccess();
            }

            lblError.Text = "";
            lblContent.Text = "";

            int _cryr = DateTime.Now.Year;
            int _styr = _cryr;
            ddlTargetYear.Items.Clear();
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlTargetYear.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        LoadGrid();
    }

    protected void btnNewTarget_Click(object sender, EventArgs e)
    {
        Response.Redirect("agent-target-amount.aspx");
    }

    protected void grvAgentTarget_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvAgentTarget.PageIndex = e.NewPageIndex;
        LoadGrid();

    }

    protected void grvAgentTarget_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[5].Font.Bold = true;
            e.Row.Cells[2].Font.Bold = true;
            e.Row.Cells[2].ForeColor = System.Drawing.Color.Green;
            e.Row.Cells[2].BackColor = System.Drawing.Color.LightGray;
            string ActiveStatus = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Status"));
            if (ActiveStatus.ToUpper() == "DEACTIVE")
            {
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
            }
        }
    }

    protected void grvAgentTarget_RowCommand(object sender, GridViewCommandEventArgs e)
    {        
        if (e.CommandName.Equals("MyEdit"))
        {
            if (e.CommandArgument != null)
            {
                Int64 BrokerId = -1;
                BrokerId = Convert.ToInt64(e.CommandArgument);
                if (BrokerId > -1)
                {
                    Response.Redirect("agent-target-amount.aspx?BrokerId=" + BrokerId.ToString());
                }
            }
        }
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        DataSet _dsSummary = new DataSet();
        DataTable _dtSummary = new DataTable();
        Int32 TargetYear = Convert.ToInt32(ddlTargetYear.Text);
        Int32 Renewal = Convert.ToInt32(ddlRenewal.Text);
        Int16 Status = Convert.ToInt16(ddlStatus.Text);

        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        string _spName = "sprocAgentTargetAmountSelectItemList";
        SortedList _sPara = new SortedList();
        _sPara.Add("@TargetYear", TargetYear);
        _sPara.Add("@Renewal", Renewal);
        _sPara.Add("@Status", Status);

        _dsSummary = _db.GetDataSet(_spName, _sPara);
        if (_dsSummary != null && _dsSummary.Tables.Count > 0 && _dsSummary.Tables[0].Rows.Count > 0)
        {
            _dtSummary = _dsSummary.Tables[0];
            grvAgentTarget.DataSource = _dtSummary;
            grvAgentTarget.DataBind();
            EnablePrintView();
        }
        else
        {
            grvAgentTarget.DataSource = null;
            grvAgentTarget.DataBind();
            EnablePrintView();
        }

    }

    private void EnablePrintView()
    {
        if (grvAgentTarget.Rows.Count > 0)
        {
            lblRecordCount.Text = "(" + grvAgentTarget.Rows.Count.ToString() + ") record(s). Date/Time: " + CE.Global.General.GetUTCDateTime().ToString("dd-MMM-yyyy hh:mm tt") + "&nbsp;&nbsp;";
        }
        else
        {
            lblRecordCount.Text = "";
        }
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            //btnDownLoad.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            //btnDownLoad.Enabled = true;
        }
    }

}
